package at.fhj.itm11.forum.service.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;

import at.fhj.itm11.forum.data.TopicDAO;
import at.fhj.itm11.forum.data.UserDAO;
import at.fhj.itm11.forum.model.Icon;
import at.fhj.itm11.forum.model.Topic;
import at.fhj.itm11.forum.service.TopicService;
import at.fhj.itm11.forum.service.converter.DTOConverter;
import at.fhj.itm11.forum.service.converter.IconDTOConverter;
import at.fhj.itm11.forum.service.converter.TopicDTOConverter;
import at.fhj.itm11.forum.service.dtos.IconDTO;
import at.fhj.itm11.forum.service.dtos.TopicDTO;
import at.fhj.itm11.forum.service.dtos.UserDTO;

@Stateless
@Named
public class TopicServiceImpl implements TopicService {
	private DTOConverter<TopicDTO, Topic> topicDtoConverter = new TopicDTOConverter();
	private DTOConverter<IconDTO, Icon> iconDtoConverter = new IconDTOConverter();


	@Inject
	@Named("topicDAOImpl")
	private TopicDAO topicDao;

	@Inject
	@Named("userDAOImpl")
	private UserDAO userDao;

	@Override
	public TopicDTO findById(Long id) {
		Topic entity = topicDao.findById(id);
		if(entity != null) {
			return topicDtoConverter.convertToDto(entity);
		}
		
		return null;
	}

	@Override
	public List<TopicDTO> retrieveAllTopicsOrderedByTitle() {
		List<Topic> entityList = topicDao.retrieveAllTopicsOrderedByTitle();
		
		if(entityList != null) {
			return topicDtoConverter.convertToDto(entityList);
		}
		
		return null;
	}

	@Override
	public void register(TopicDTO topicDto, UserDTO userDto, Date date) {
		Topic t = new Topic();

		t.setId(topicDto.getId());
		t.setTitle(topicDto.getTitle());
		t.setDescription(topicDto.getDescription());
		t.setIcon(iconDtoConverter.convertToEntity(topicDto.getIcon()));
		t.setAuthor(userDao.findById(userDto.getId()));
		t.setDate(date);

		topicDao.register(t);
	}

	public void setTopicDao(TopicDAO topicDao) {
		this.topicDao = topicDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

}
